import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('cart', () => {
  let carts=ref([])
  let id=ref([])
  function addcart(cartdata,goodsid){
    let username=localStorage.getItem('username')
    let usercart=carts.value.filter(i=>i.username==username)
    let pdcart=usercart.find(i=>i.goods.goods_id==goodsid)
    if(pdcart){
      pdcart.number++
    }
    else{
      carts.value.push({
        username:username,
        id:Math.floor(Math.random()*100000),
        number:1,
        goods:cartdata,
        checked:false
      })
    }
  }
  function delcart(id){
    carts.value=carts.value.filter(i=>i.id!=id)
  }

  return { carts, addcart, delcart}
},
{
    persist: true,
  },
)
